#include "screenwnd.h"
#include "ui_screenwnd.h"

#include <QList>
#include <QDebug>

#include <qtablewidget.h>
#include <QPushButton>

#include <QFileDialog>  
#include <QLabel>  
#include <opencv2/opencv.hpp>  


 static cv::Mat image;
ScreenWnd::ScreenWnd(QTabWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScreenWnd)
	,tab_(parent)
{
    ui->setupUi(this);
	connect ( parent, SIGNAL(currentChanged ( int )), this, SLOT(indexChanged(int) ) );

	QPushButton * btn = this->findChild<QPushButton*>("openImg");
	connect ( btn, SIGNAL(clicked ( )), this, SLOT(open() ) );
}

ScreenWnd::~ScreenWnd()
{
    delete ui;
}

void ScreenWnd::indexChanged(int index){
	QWidget* maybeThis = tab_->widget( index);
	if ( maybeThis->objectName() == "") {
		return;
	}

}

void ScreenWnd::open(){
	//cv::Mat image; 
	QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.jpeg *.bmp *.avi)"));  
 //   image = cv::imread(fileName.toAscii().data());  
 //   //cv::namedWindow("Original Image");  
 //   //cv::imshow("Original Image", image);
	//if(!image.data)
 //   {
 //       qDebug()<<"No data!--Exiting the program ";
 //   }else{
	//	cv::cvtColor(image, image, CV_BGR2RGB);
	//	QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);  
	//	QLabel * label = this->findChild<QLabel*>("img");
	//	//label->move(200, 50);  
	//	label->setPixmap(QPixmap::fromImage(img));  
	//	label->resize(label->pixmap()->size());      
	//	label->show();  
	//}


	cv::VideoCapture capture(0);  
    cv::VideoWriter writer(fileName.toAscii().data(), CV_FOURCC('M', 'J', 'P', 'G'), 25.0, cv::Size(640, 480));  
    cv::Mat frame;  
      
    while (capture.isOpened())  
    {  
        capture >> frame;  
        writer << frame;  
        imshow("video", frame);  
        if (cvWaitKey(20) == 27)  
        {  
            break;  
        }  
    } 
}